package com.sdut.material.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdut.material.mapper.BuyOrderMapper;
import com.sdut.material.mapper.RepositoryMapper;
import com.sdut.material.pojo.*;
import com.sdut.material.mapper.InputMapper;
import com.sdut.material.pojo.vo.InputVO;
import com.sdut.material.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdut.material.util.JSONResult;
import com.sdut.material.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yu
 * @since 2022-04-20
 */
@Service
public class InputServiceImpl extends ServiceImpl<InputMapper, Input> implements IInputService {
    @Autowired
    private InputMapper inputMapper;
    @Autowired
    private ISupplierMaterialService supplierMaterialService;
    @Autowired
    private ISupplierService supplierService;
    @Autowired
    private IMaterialService materialService;
    @Autowired
    private IRepositoryService repositoryService;
    @Autowired
    private BuyOrderMapper buyOrderMapper;
    @Override
    public LayUITableResult selectByPage(Long page, Long limit) {
        Long offset = (page - 1) * limit;
        List<Input> inputList = inputMapper.selectByPage(offset,limit);
        int totalCount = inputMapper.selectTotalCount();
        List<InputVO> inputVOList = new ArrayList<>();
        for (Input input : inputList) {
            InputVO inputVO = new InputVO();
            SupplierMaterial supplierMaterial = supplierMaterialService.selectById(input.getSumaId());
            Supplier supplier = supplierService.selectById(supplierMaterial.getSupplierId());
            Material material = materialService.selectById(supplierMaterial.getMaterialId());
            inputVO.setId(input.getId());
            inputVO.setSupplierName(supplier.getName());
            inputVO.setMaterialName(material.getName());
            inputVO.setCount(input.getCount());
            inputVO.setShelflife(input.getShelflife());
            inputVO.setStatus(input.getStatus());
            inputVOList.add(inputVO);
        }
        return LayUITableResult.ok((long) totalCount,inputVOList);
    }

    @Override
    public void insert(Input input) {
        inputMapper.insert(input);
    }

    @Override
    public JSONResult inputRepository(Integer inputId) {
        Input input = inputMapper.selectById(inputId);
        int supplierMaterialId = input.getSumaId();
        Repository repository = repositoryService.selectBySupplierMaterialId(supplierMaterialId);
        if (repository != null) {
            int count = repository.getCount();
            repository.setCount(count+input.getCount());
            repositoryService.updateRepository(repository);
        } else {
            Repository newRepository = new Repository();
            newRepository.setSumaId(input.getSumaId());
            newRepository.setCount(input.getCount());
            newRepository.setShelflife(input.getShelflife());
            repositoryService.insert(newRepository);
        }
        //更新订单状态
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("suma_id",input.getSumaId());
        queryWrapper.eq("count",input.getCount());
        queryWrapper.eq("status",2);
        BuyOrder buyOrder = buyOrderMapper.selectOne(queryWrapper);
        buyOrder.setStatus(3);
        buyOrderMapper.updateById(buyOrder);
        //同时更新入库状态
        input.setStatus(2);
        inputMapper.updateById(input);
        return JSONResult.ok("入库成功");
    }

    @Override
    public void cancelById(Integer inputId) {
        Input input = inputMapper.selectById(inputId);
        input.setStatus(3);
        inputMapper.updateById(input);
    }
}
